package com.fsd.experiment6_0;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_research = findViewById(R.id.btn_search);
        final EditText edt = findViewById(R.id.edt_city);
        final TextView tv_num = findViewById(R.id.tv_num);
        final TextView tv_area = findViewById(R.id.tv_area);
        final TextView tv_ct = findViewById(R.id.tv_ct);
        final TextView tv_quality = findViewById(R.id.tv_quality);

        btn_research.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String city = edt.getText().toString();
                API apiObject = new API();
                apiObject.api(city,tv_area,tv_num,tv_quality,tv_ct);

            }
        });
    }
}
class API{
    String host = "https://ali-pm25.showapi.com";
    String path = "/pm25-detail";
    String method = "GET";
    String appcode = "28349a645182420e94cc14c16bc024d6";

    public void api(final String city, final TextView tv1, final TextView tv2, final TextView tv3, final TextView tv4){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        StringBuilder response = new StringBuilder();
                        String requestCity = city;
                        URL url = new URL(host +path+ "?city=" + requestCity);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestProperty("Authorization", "APPCODE " + appcode);
                        connection.setRequestMethod(method);
                        InputStream in = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
                        String line;
                        while ((line = reader.readLine())!=null) {
                            response.append(line);
                        }
                        reader.close();
                        connection.disconnect();
                        parseJson(response.toString(),tv1,tv2,tv3,tv4);
                } catch (MalformedURLException e) {
                    e.printStackTrace();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                }
            }).start();

    }

    public void parseJson(String json,TextView tv1,TextView tv2,TextView tv3,TextView tv4) {
              try {
                  JSONObject jsonObject = new JSONObject(json);
                  JSONObject showapi_res_body = new JSONObject(jsonObject.getString("showapi_res_body"));
                  JSONObject pm = new JSONObject(showapi_res_body.getString("pm"));
                  String num = pm.getString("aqi");
                  String area = pm.getString("area");
                  String ct = pm.getString("ct");
                  String quality = pm.getString("quality");

                  tv1.setText("城市：  "+area);
                  tv2.setText("空气质量指数：  "+num);
                  tv3.setText("空气质量：  "+quality);
                  tv4.setText("更新时间：  "+ct);
              } catch (JSONException e) {
                  e.printStackTrace();
              }
    }
}
//class API {
//    String host = "https://ali-pm25.showapi.com";
//    String path = "/pm25-detail";
//    String method = "GET";
//    String appcode = "28349a645182420e94cc14c16bc024d6";
//    public void Request(){
//
//        Map<String, String> headers = new HashMap<String, String>();
//        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
//        headers.put("Authorization", "APPCODE " + appcode);
//        Map<String, String> querys = new HashMap<String, String>();
//        querys.put("city", "天津");
//
//
//        try {
//            /**
//             * 重要提示如下:
//             * HttpUtils请从
//             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
//             * 下载
//             *
//             * 相应的依赖请参照
//             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
//             */
//            System.out.println("-------------------------------");
//            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
//
//            System.out.println(response.toString());
//            //获取response的body
//            //System.out.println(EntityUtils.toString(response.getEntity()));
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        }
//    }
//}
